<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BoxGiftboxitem;
use Dcat\Admin\Support\JavaScript;
use Illuminate\Support\Str;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxGiftboxitemController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BoxGiftboxitem(), function (Grid $grid) {
            $grid->disableViewButton(true);
            $grid->column('id')->sortable();
            $grid->column('title')->display(function ($content) {
                $limit = 10; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($content, 'utf-8') > $limit) {
                    $content = mb_substr($content, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $content;
            })->width('15%');
            // $grid->column('image')->image('http://laraveDact.com/uploads', 50, 50);
            $grid->column('image')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $grid->column('details')->display(function ($content) {
                $limit = 50; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($content, 'utf-8') > $limit) {
                    $content = mb_substr($content, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $content;
            })->width('25%');
            $grid->column('money');
            $grid->column('class')->using(['1' => '小班', '2' => '中班', '3' => '大班'])->badge([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default    
                1 => 'primary',
                2 => 'danger',
                3 => 'success',
            ]);

            $grid->column('created')->width('10%')->sortable();
            $grid->column('updated')->width('10%')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BoxGiftboxitem(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            // $show->field('image')->image('http://laraveDact.com/uploads', 100, 100);
            $show->column('image')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $show->field('details');
            $show->field('money');
            $show->field('class')->using(['1' => '小班', '2' => '中班', '3' => '大班']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxGiftboxitem(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->text('money')->required();
            $form->image('image')->disk('public')->accept('jpg,png,gif,jpeg')->maxSize(512)->required()->autoUpload()->help('大小不要超过512K');
            $form->markdown('details')->options([
                'onpreviewing' => JavaScript::make(
                    <<<JS
            function() {
                // console.log("onpreviewing =>", this, this.id, this.settings);
                // on previewing you can custom css .editormd-preview-active
            }
            JS
                ),
            ])->disk('public')->required();
            $form->radio('class')->required()->options([1 => '小班', 2 => '中班', 3 => '大班'])->default(1);
            $form->date('created')->format('YYYY-MM-DD')->default(date('Y-m-d'));
            $form->date('updated')->format('YYYY-MM-DD');
        });
    }
}
